package com.offcn.user.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.user.po.TMemberAddress;
import com.offcn.user.service.UserService;
import com.offcn.user.vo.resp.UserAddressVo;
import com.offcn.user.vo.resp.UserRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags="获取会员信息/更新个人信息/获取用户收货地址")
@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation("根据用户请求令牌token获取用户地址信息")
    @ApiImplicitParam(value = "访问令牌", name = "accessToken", required = true)
    @GetMapping("/info/address")
    public AppResponse<List<UserAddressVo>> address(String accessToken){
        //根据token从redis读取对应的用户编号
        String memberId = redisTemplate.opsForValue().get(accessToken);

        //根据用户编号，读取对应地址集合
        List<TMemberAddress> memberAddressList = userService.addressList(Integer.valueOf(memberId));

        List<UserAddressVo> userAddressVoList=new ArrayList<>();
        //遍历查询到地址集合
        for (TMemberAddress memberAddress : memberAddressList) {
            UserAddressVo vo = new UserAddressVo();
            vo.setId(memberAddress.getId());
            vo.setAddress(memberAddress.getAddress());
            userAddressVoList.add(vo);
        }
        return AppResponse.ok(userAddressVoList);
    }

    //根据用户id获取用户详细信息
    @GetMapping("findUser/{id}")
    public AppResponse<UserRespVo> findUserById(@PathVariable("id") Integer id){
        UserRespVo userRespVo = userService.findUserById(id);
        return AppResponse.ok(userRespVo);
    }
}
